package com.apikey.auth.service.impl;

import com.apikey.auth.entity.OperationHistory;
import com.apikey.auth.repository.OperationHistoryRepository;
import com.apikey.auth.service.OperationHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * 操作历史服务实现类
 * 提供操作历史的管理功能实现
 *
 * @author API Key Framework
 * @version 1.0
 */
@Service
@Transactional
public class OperationHistoryServiceImpl implements OperationHistoryService {

    @Autowired
    private OperationHistoryRepository operationHistoryRepository;

    /**
     * 保存操作历史
     *
     * @param operationHistory 操作历史
     * @return 保存的操作历史
     */
    @Override
    public OperationHistory save(OperationHistory operationHistory) {
        return operationHistoryRepository.save(operationHistory);
    }

    /**
     * 记录操作历史
     *
     * @param apiKeyId      API Key ID
     * @param operationType 操作类型
     * @param operatorId    操作员ID
     * @param description   描述
     * @return 操作历史
     */
    @Override
    public OperationHistory recordOperation(Long apiKeyId, String operationType, String operatorId, String description) {
        OperationHistory history = new OperationHistory(apiKeyId, operationType, operatorId, description);
        return save(history);
    }

    /**
     * 记录操作历史（包含旧值和新值）
     *
     * @param apiKeyId      API Key ID
     * @param operationType 操作类型
     * @param operatorId    操作员ID
     * @param description   描述
     * @param oldValue      旧值
     * @param newValue      新值
     * @return 操作历史
     */
    @Override
    public OperationHistory recordOperation(Long apiKeyId, String operationType, String operatorId,
                                            String description, String oldValue, String newValue) {
        OperationHistory history = new OperationHistory(apiKeyId, operationType, operatorId, description);
        history.setOldValue(oldValue);
        history.setNewValue(newValue);
        return save(history);
    }

    /**
     * 根据API Key ID查找操作历史
     *
     * @param apiKeyId API Key ID
     * @param pageable 分页参数
     * @return 操作历史分页列表
     */
    @Override
    @Transactional(readOnly = true)
    public Page<OperationHistory> findByApiKeyId(Long apiKeyId, Pageable pageable) {
        return operationHistoryRepository.findByApiKeyId(apiKeyId, pageable);
    }

    /**
     * 根据操作员ID查找操作历史
     *
     * @param operatorId 操作员ID
     * @param pageable   分页参数
     * @return 操作历史分页列表
     */
    @Override
    @Transactional(readOnly = true)
    public Page<OperationHistory> findByOperatorId(String operatorId, Pageable pageable) {
        return operationHistoryRepository.findByOperatorId(operatorId, pageable);
    }

    /**
     * 根据操作类型查找操作历史
     *
     * @param operationType 操作类型
     * @param pageable      分页参数
     * @return 操作历史分页列表
     */
    @Override
    @Transactional(readOnly = true)
    public Page<OperationHistory> findByOperationType(String operationType, Pageable pageable) {
        return operationHistoryRepository.findByOperationType(operationType, pageable);
    }

    /**
     * 查找所有操作历史
     *
     * @param pageable 分页参数
     * @return 操作历史分页列表
     */
    @Override
    @Transactional(readOnly = true)
    public Page<OperationHistory> findAll(Pageable pageable) {
        return operationHistoryRepository.findAll(pageable);
    }

    /**
     * 删除指定天数之前的操作历史
     *
     * @param days 天数
     * @return 删除的记录数
     */
    @Override
    public int deleteOldRecords(int days) {
        LocalDateTime cutoffTime = LocalDateTime.now().minusDays(days);
        return operationHistoryRepository.deleteByCreatedAtBefore(cutoffTime);
    }
}